package com.ken.apollo.controller;

import com.ken.apollo.commons.result.R;
import com.ken.apollo.domain.bo.MailBatchSendBo;
import com.ken.apollo.domain.query.MailBatchSendQuery;
import com.ken.apollo.domain.vo.MailBatchSendVo;
import com.ken.apollo.service.MailBatchSendService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 邮件群发控制器
 *
 * @author Ken
 * @since 2025-08-25
 */
@Controller
@RequestMapping("/api/mail")
@Slf4j
public class MailController {

    @Resource
    private MailBatchSendService mailBatchSendService;

    /**
     * 创建邮件群发任务
     */
    @PostMapping("/batch/create")
    @ResponseBody
    public R<Boolean> createBatchSendTask(@RequestBody MailBatchSendBo mailBatchSendBo) {
        try {
            boolean result = mailBatchSendService.createBatchSendTask(mailBatchSendBo);
            return R.success(result);
        } catch (Exception e) {
            log.error("【创建邮件群发任务失败】", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 开始执行群发任务
     */
    @PostMapping("/batch/start/{batchId}")
    @ResponseBody
    public R<Boolean> startBatchSend(@PathVariable("batchId") Long batchId) {
        try {
            boolean result = mailBatchSendService.startBatchSend(batchId);
            return R.success(result);
        } catch (Exception e) {
            log.error("【开始执行群发任务失败】任务ID：{}", batchId, e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 取消群发任务
     */
    @PostMapping("/batch/cancel/{batchId}")
    @ResponseBody
    public R<Boolean> cancelBatchSend(@PathVariable("batchId") Long batchId) {
        try {
            boolean result = mailBatchSendService.cancelBatchSend(batchId);
            return R.success(result);
        } catch (Exception e) {
            log.error("【取消群发任务失败】任务ID：{}", batchId, e);
            return R.fail(e.getMessage());
        }
    }
    
    /**
     * 重试群发任务
     */
    @PostMapping("/batch/retry/{batchId}")
    @ResponseBody
    public R<Boolean> retryBatchSend(@PathVariable("batchId") Long batchId) {
        try {
            boolean result = mailBatchSendService.retryBatchSend(batchId);
            return R.success(result);
        } catch (Exception e) {
            log.error("【重试群发任务失败】任务ID：{}", batchId, e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 查询邮件群发任务列表
     */
    @GetMapping("/batch/list")
    @ResponseBody
    public R<List<MailBatchSendVo>> listBatchSendTasks(MailBatchSendQuery query) {
        try {
            List<MailBatchSendVo> result = mailBatchSendService.listBatchSendTasks(query);
            return R.success(result);
        } catch (Exception e) {
            log.error("【查询邮件群发任务列表失败】", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 根据ID查询邮件群发任务详情
     */
    @GetMapping("/batch/detail/{id}")
    @ResponseBody
    public R<MailBatchSendVo> getBatchSendTaskById(@PathVariable("id") Long id) {
        try {
            MailBatchSendVo result = mailBatchSendService.getBatchSendTaskById(id);
            return R.success(result);
        } catch (Exception e) {
            log.error("【查询邮件群发任务详情失败】任务ID：{}", id, e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 邮件管理页面
     */
    @GetMapping("/backs/mail_manager")
    public String mailManager() {
        return "backs/mail_manager";
    }
}